package com.jianan.mis.app.overseastudy;

import static com.jianan.mis.util.ui.calendar.SWTCalendarWrapper.openCalendar;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.jianan.mis.data.domain.Country;
import com.jianan.mis.data.domain.School;
import com.jianan.mis.service.CountryService;
import com.jianan.mis.sys.SpringBeanFactory;

public class SchoolDialog extends Dialog {
	
	private Text txtCopartner;
	private StyledText txtSpeciality;
	private Text txtEnterRequirement;
	private Button btnPhoneInterview;
	private Button btnDoubleMatriculate;
	private Text txtWebsite;
	private Text txtCity;
	private Text txtState;
	private StyledText txtRemark;
	private Text txtScholarship;
	private Text txtOverseaStudentRate;
	private Text txtStudentCount;
	private Text txtRank;
	private Text txtGraduateRate;
	private Text txtApplyEndDate;
	private Text txtApplyFee;
	private Text txtFee;
	private Text txtLangReq;
	private Combo comboProperty;
	private Combo comboPublic;
	private Combo countryCombo;
	private Text txtName;
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	class CountryListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			return ((Country) element).getName();
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	
	private ComboViewer countryComboViewer;
	
	private String[] properties = new String[] {"语言", "高中", "衔接与预科", "大学", "研究生", "寄宿", "A-Levels", "研究生预科"};

	final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");

	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public SchoolDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginLeft = 25;
		gridLayout.marginTop = 10;
		gridLayout.marginWidth = 0;
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);
		
		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("学校名称:");

		txtName = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtName = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtName.widthHint = 120;
		txtName.setLayoutData(gd_txtName);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_3.setText("国家:");

		countryComboViewer = new ComboViewer(container, SWT.READ_ONLY);
		countryComboViewer.setLabelProvider(new CountryListLabelProvider());
		countryComboViewer.setContentProvider(new ContentProvider());
		countryCombo = countryComboViewer.getCombo();
		countryCombo.setEnabled(false);
		final GridData gd_countryCombo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		countryCombo.setLayoutData(gd_countryCombo);
		countryComboViewer.setInput(countryService.getAll());

		final Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label.setText("合作方:");

		txtCopartner = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtCopartner = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtCopartner.widthHint = 100;
		txtCopartner.setLayoutData(gd_txtCopartner);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_4.setText("公私立:");

		comboPublic = new Combo(container, SWT.READ_ONLY);
		comboPublic.setEnabled(false);
		comboPublic.setItems(new String[] {"公立", "私立"});
		final GridData gd_comboPublic = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboPublic.setLayoutData(gd_comboPublic);

		final Label label_5 = new Label(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_5.setText("学校性质:");

		comboProperty = new Combo(container, SWT.READ_ONLY);
		comboProperty.setEnabled(false);
		comboProperty.setItems(properties);
		final GridData gd_comboProperty = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboProperty.setLayoutData(gd_comboProperty);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_1.setText("所在州:");

		txtState = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtState = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtState.widthHint = 120;
		txtState.setLayoutData(gd_txtState);

		final Label label_17 = new Label(container, SWT.NONE);
		label_17.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_17.setText("所在城市:");

		txtCity = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtCity = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtCity.widthHint = 120;
		txtCity.setLayoutData(gd_txtCity);

		final Label label_23 = new Label(container, SWT.NONE);
		label_23.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_23.setText("双录取:");

		btnDoubleMatriculate = new Button(container, SWT.CHECK);
		btnDoubleMatriculate.setEnabled(false);

		final Label label_24 = new Label(container, SWT.NONE);
		label_24.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_24.setText("电话面试:");

		btnPhoneInterview = new Button(container, SWT.CHECK);
		btnPhoneInterview.setEnabled(false);

		final Label label_25 = new Label(container, SWT.NONE);
		label_25.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_25.setText("入学要求:");

		txtEnterRequirement = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtEnterRequirement = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtEnterRequirement.widthHint = 420;
		txtEnterRequirement.setLayoutData(gd_txtEnterRequirement);

		final Label label_26 = new Label(container, SWT.NONE);
		label_26.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_26.setText("专业:");

		txtSpeciality = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
		txtSpeciality.setWordWrap(true);
		final GridData gd_txtSpeciality = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtSpeciality.heightHint = 50;
		gd_txtSpeciality.widthHint = 325;
		txtSpeciality.setLayoutData(gd_txtSpeciality);

		final Label label_7 = new Label(container, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_7.setText("学费:");

		txtFee = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtFee = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtFee.widthHint = 100;
		txtFee.setLayoutData(gd_txtFee);

		final Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_6.setText("语言要求:");

		txtLangReq = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtLangReq = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtLangReq.widthHint = 130;
		txtLangReq.setLayoutData(gd_txtLangReq);

		final Label label_8 = new Label(container, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_8.setText("申请费:");

		txtApplyFee = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtApplyFee = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtApplyFee.widthHint = 100;
		txtApplyFee.setLayoutData(gd_txtApplyFee);

		final Label label_9 = new Label(container, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_9.setText("申请截止日期:");

		txtApplyEndDate = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		txtApplyEndDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtApplyEndDate, dateFormat);
			}
		});
		txtApplyEndDate.setEditable(false);
		final GridData gd_txtApplyEndDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtApplyEndDate.widthHint = 90;
		txtApplyEndDate.setLayoutData(gd_txtApplyEndDate);

		final Label label_10 = new Label(container, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_10.setText("毕业率(%):");

		txtGraduateRate = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtGraduateRate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtGraduateRate.setLayoutData(gd_txtGraduateRate);

		final Label label_11 = new Label(container, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_11.setText("学校排名:");

		txtRank = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtRank = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtRank.setLayoutData(gd_txtRank);

		final Label label_12 = new Label(container, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_12.setText("学生人数:");

		txtStudentCount = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtStudentCount = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtStudentCount.setLayoutData(gd_txtStudentCount);

		final Label label_13 = new Label(container, SWT.NONE);
		label_13.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_13.setText("海外学生比例(%):");

		txtOverseaStudentRate = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtOverseaStudentRate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtOverseaStudentRate.setLayoutData(gd_txtOverseaStudentRate);

		final Label label_14 = new Label(container, SWT.NONE);
		label_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_14.setText("奖学金:");

		txtScholarship = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtScholarship = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtScholarship.widthHint = 210;
		txtScholarship.setLayoutData(gd_txtScholarship);

		final Label label_16 = new Label(container, SWT.NONE);
		label_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_16.setText("网址:");

		txtWebsite = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_txtWebsite = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtWebsite.widthHint = 280;
		txtWebsite.setLayoutData(gd_txtWebsite);

		final Label label_15 = new Label(container, SWT.NONE);
		label_15.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		label_15.setText("备注:");

		txtRemark = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		txtRemark.setWordWrap(true);
		final GridData gd_txtRemark = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtRemark.heightHint = 50;
		gd_txtRemark.widthHint = 325;
		txtRemark.setLayoutData(gd_txtRemark);
		
		showSchoolFields();
		//
		return container;
	}
	
	private CountryService countryService = (CountryService) SpringBeanFactory.getBean("countryService");
	
	private School school;
	
	public void setSchool(School school) {
		this.school = school;
	}
	
	@SuppressWarnings("unchecked")
	private void showSchoolFields() {
		txtName.setText(school.getName());
		if (school.getCountry() != null && countryComboViewer.getInput() != null) {
			List<Country> countries = (List<Country>) countryComboViewer.getInput();
			for (int i = 0; i < countries.size(); i++) {
				Country country = countries.get(i);
				if (country.getId().equals(school.getCountry().getId())) {
					countryCombo.select(i);
				}
			}
		}
		txtCopartner.setText(school.getCopartner());
		if (school.isPublic()) {
			comboPublic.select(0);
		} else {
			comboPublic.select(1);
		}
		comboProperty.select(ArrayUtils.indexOf(comboProperty.getItems(), school.getProperty()));
		if (school.getLangReq() != null) {
			txtLangReq.setText(school.getLangReq());
		} else {
			txtLangReq.setText("");
		}
		if (school.getState() != null) {
			txtState.setText(school.getState());
		} else {
			txtState.setText("");
		}
		if (school.getCity() != null) {
			txtCity.setText(school.getCity());
		} else {
			txtCity.setText("");
		}
		btnDoubleMatriculate.setSelection(school.isDoubleMatriculate());
		btnPhoneInterview.setSelection(school.isPhoneInterview());
		if (school.getEnterRequirement() != null) {
			txtEnterRequirement.setText(school.getEnterRequirement());
		} else {
			txtEnterRequirement.setText("");
		}
		if (school.getSpeciality() != null) {
			txtSpeciality.setText(school.getSpeciality());
		} else {
			txtSpeciality.setText("");
		}
		txtFee.setText(school.getFee().toString());
		if (school.getApplyFee() != null)
			txtApplyFee.setText(school.getApplyFee().toString());
		else
			txtApplyFee.setText("");
		if (school.getApplyEndDate() != null)
			txtApplyEndDate.setText(dateFormat.format(school.getApplyEndDate()));
		else
			txtApplyEndDate.setText("");
		if (school.getGraduateRate() != null)
			txtGraduateRate.setText(school.getGraduateRate().toString());
		else
			txtGraduateRate.setText("");
		if (school.getRank() != null)
			txtRank.setText(school.getRank().toString());
		else
			txtRank.setText("");
		if (school.getStudentCount() != null)
			txtStudentCount.setText(school.getStudentCount().toString());
		else
			txtStudentCount.setText("");
		if (school.getOverseaStudentRate() != null)
			txtOverseaStudentRate.setText(school.getOverseaStudentRate().toString());
		else
			txtOverseaStudentRate.setText("");
		if (school.getScholarship() != null)
			txtScholarship.setText(school.getScholarship());
		else
			txtScholarship.setText("");
		if (school.getRemark() != null)
			txtRemark.setText(school.getRemark());
		else
			txtRemark.setText("");
		if (school.getWebsite() != null)
			txtWebsite.setText(school.getWebsite());
		else
			txtWebsite.setText("");
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(552, 540);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("学校信息");
	}

}
